package cn.juque.lucenecandy.core.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.juque.lucenecandy.core.constants.StrConstant;
import lombok.Data;
import lombok.ToString;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;

import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>IdQueryWrapperBuilder</li>
 * </ul>
 * @date 2023-12-26
 **/
@Data
@ToString
public class DeleteByIdsWrapperBuilder<T extends BaseEntity> extends AbstractBuilder<T, DeleteByIdsWrapperBuilder<T>> {

    private List<String> idList;

    public DeleteByIdsWrapperBuilder(Class<T> indexClass, List<String> ids) {
        super(indexClass);
        if (CollUtil.isEmpty(ids)) {
            throw new IllegalArgumentException("ids is null");
        }
        this.idList = ids;
        ids.stream()
                .distinct()
                .forEach(id -> super.queryMap.put(new TermQuery(new Term(StrConstant.D_ID, id)),
                        BooleanClause.Occur.MUST));
    }

    @Override
    public DeleteByIdsWrapperBuilder<T> addSort(Sfunction<T, ?> sfunction, SortField.Type type, Boolean reverse) {
        return super.thisType;
    }

    @Override
    public DeleteByIdsWrapper build() {
        return new DeleteByIdsWrapper(ClassUtil.getClassName(super.indexClass, false),
                super.indexInfo, this.idList, super.queryMap, super.fieldToLoad);
    }
}
